#include <typeindex>
#include <typeinfo>
#include <unordered_map>
#include <iostream>
#include <memory>

class Base {
public:
    virtual ~Base() {}
    virtual void doWork() = 0;
};

class Derived1 : public Base {
public:
    void doWork() override { std::cout << "Derived1 working." << std::endl; }
};

class Derived2 : public Base {
public:
    void doWork() override { std::cout << "Derived2 working." << std::endl; }
};

int main() {
    std::unordered_map<std::type_index, std::unique_ptr<Base>> factory;

    // 使用 type_index 来索引不同的派生类
    factory[std::type_index(typeid(Derived1))] = std::make_unique<Derived1>();
    factory[std::type_index(typeid(Derived2))] = std::make_unique<Derived2>();

    // 调用各个对象的 doWork 函数
    for (auto& pair : factory) {
        pair.second->doWork();
    }

    return 0;
}